package com.jack.lib.ui.widget

import android.content.Context
import com.jack.lib.core.ext.MM
import kotlin.jvm.JvmOverloads
import android.content.res.TypedArray
import android.util.AttributeSet
import com.jack.lib.ui.R
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.View.MeasureSpec
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.jack.lib.core.utils.JItemDecoration
import com.jack.lib.core.utils.ReflexUtils

/**
 * 作者: jack(黄冲)
 * 邮箱: 907755845@qq.com
 * create on 2018/11/20 14:29
 */
open class JRecyclerView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int = 0
) : RecyclerView(context, attrs, defStyle) {

    private var mAutoHeight = false
    private var mHorizontal = false
    private var mReverse = false
    private var mSpanCount = 0
    private var mEmptyLayout = 0
    private var mInterceptMode = InterceptMode.NONE
    private var mDisableTouchEvent = false

    enum class InterceptMode {
        INTERCEPT, NOT_INTERCEPT, NONE
    }

    init {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.JRecyclerView)
        parseAttr(typedArray)
        typedArray.recycle()
        init()
    }

    private fun parseAttr(typedArray: TypedArray) {
        mAutoHeight = typedArray.getBoolean(R.styleable.JRecyclerView_JAutoHeight, false)
        val dimension = typedArray.getDimension(R.styleable.JRecyclerView_JDividerHeight, 0f)
        mHorizontal = typedArray.getBoolean(R.styleable.JRecyclerView_JHorizontal, false)
        mReverse = typedArray.getBoolean(R.styleable.JRecyclerView_JReverse, false)
        mSpanCount = typedArray.getInt(R.styleable.JRecyclerView_JSpanCount, 1)
        mEmptyLayout = typedArray.getResourceId(R.styleable.JRecyclerView_JEmptyLayout, -1)
        require(mSpanCount > 0) { "spanCount 不能 <= 0" }
        if (mHorizontal) {
            addItemDecoration(JItemDecoration(0, 0, 0, 0, 0, dimension.MM.toInt()))
        } else {
            addItemDecoration(JItemDecoration(0, 0, 0, 0, dimension.MM.toInt(), 0))
        }
        val autoRoll = typedArray.getBoolean(R.styleable.JRecyclerView_JAutoRoll, false)
        if (autoRoll) {
            setHasFixedSize(true)
            isFocusable = false
            isNestedScrollingEnabled = false
        }
    }

    override fun setAdapter(adapter: Adapter<*>?) {
        super.setAdapter(adapter)
        if (adapter != null && mEmptyLayout != -1 && ReflexUtils.`is`(adapter.javaClass, "BaseQuickAdapter")) {
            ReflexUtils.setMethod<Any>(adapter, adapter.javaClass, "setEmptyView", ReflexUtils.ClassParam(
                    LayoutInflater.from(
                        context
                    ).inflate(mEmptyLayout, null), View::class.java
                )
            )
        }
    }

    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        return if (mDisableTouchEvent) {
            false
        } else super.dispatchTouchEvent(ev)
    }

    override fun onInterceptTouchEvent(e: MotionEvent): Boolean {
        return when (mInterceptMode) {
            InterceptMode.INTERCEPT -> true
            InterceptMode.NOT_INTERCEPT -> false
            else -> super.onInterceptTouchEvent(e)
        }
    }

    fun setInterceptEvent(mode: InterceptMode) {
        mInterceptMode = mode
    }

    fun setDisableTouchEvent(disableTouchEvent: Boolean) {
        mDisableTouchEvent = disableTouchEvent
    }

    private fun init() {
        layoutManager = if (mSpanCount == 1) {
            JLinearLayoutManager(
                context,
                if (mHorizontal) LinearLayoutManager.HORIZONTAL else LinearLayoutManager.VERTICAL,
                mReverse
            )
        } else {
            GridLayoutManager(
                context,
                mSpanCount,
                if (mHorizontal) GridLayoutManager.HORIZONTAL else GridLayoutManager.VERTICAL,
                mReverse
            )
        }
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        if (mAutoHeight) {
            val expandSpec = MeasureSpec.makeMeasureSpec(Int.MAX_VALUE shr 2, MeasureSpec.AT_MOST)
            super.onMeasure(widthMeasureSpec, expandSpec)
            return
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    }


}